/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * searchproductname_in_addstockorder.java
 *
 * Created on Nov 10, 2011, 9:37:51 PM
 */

package giormi;

import javax.swing.JFrame;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Ling
 */
public class searchproductname_in_addstockorder extends javax.swing.JFrame {

    /** Creates new form searchproductname_in_addstockorder */
    private JFrame frame;
    private addOrder order;
    private addStock stock;
    private String stockorder;

    private Connection conn;
    private String SQLStatement;
    private String URL;
    private Statement stmt;
    private ResultSet rset;

    public searchproductname_in_addstockorder(JFrame os, String rec) {
        initComponents();
        this.setLocation(400, 200);
        this.setSize(460, 400);
        this.setResizable(false);
        this.setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);
        frame = os;
        this.stockorder = rec;
        display();
    }

    public void display(){
        connect();
        try {
            int i=0;

            if(stockorder.equalsIgnoreCase("stock"))
                SQLStatement = "SELECT DISTINCT(p_name) FROM product WHERE p_status = 'Active'";
            else SQLStatement = "SELECT DISTINCT(p_name) FROM product";
            
            rset = stmt.executeQuery(SQLStatement);
            while (rset.next()) {
                productnameTable.setValueAt(rset.getString("p_name"), i, 0);
                i++;
            }
            rset.close();
            disconnect();
        } catch (SQLException ex) {
            Logger.getLogger(searchproductname_in_addstockorder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int gettablerows(){
        int total=0;
        connect();
        try {
            if(stockorder.equalsIgnoreCase("stock"))
                // statement for products (in stock record) count
                SQLStatement = "SELECT COUNT(DISTINCT(p_name)) from product";

            // statement for products (in orderr record) count
            else SQLStatement = "SELECT COUNT(DISTINCT(p_name)) from product WHERE p_status = 'Active'";

            rset = stmt.executeQuery(SQLStatement);
            while (rset.next()) {
                total = rset.getInt(1); // store result to total
            }
            rset.close();
            disconnect();
        } catch (SQLException ex) {
            Logger.getLogger(searchproductname_in_addstockorder.class.getName()).log(Level.SEVERE, null, ex);
        }
        return total;
    }

    public void erase(){
        for (int i=0; i<gettablerows(); i++) // row count
            productnameTable.setValueAt("", i, 0);
    }

    public void connect(){
        try{
            DBConnection db = new DBConnection();
            conn = DBConnection.get();
            stmt = conn.createStatement();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
            JOptionPane.showMessageDialog(null,e.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    public void disconnect(){
        try{
            stmt.close();
            conn.close();
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR", JOptionPane.WARNING_MESSAGE);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        productnameTable = new javax.swing.JTable();
        sp_searchproductnameButton = new javax.swing.JButton();
        sp_searchproductnameField = new javax.swing.JTextField();
        sp_okButton = new javax.swing.JButton();
        sp_cancelButton = new javax.swing.JButton();
        sp_resetviewButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Search a Product");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        productnameTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Heng Bing Whole Mushroom"},
                {"Heng Bing Straw Mushroom"},
                {"Heng Bing Pieches and Stem Mushroom"},
                {"Heng Bing Oyster Sauce"},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Product Name"
            }
        ));
        jScrollPane1.setViewportView(productnameTable);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, 410, 200));

        sp_searchproductnameButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Zoom-icon (1).png"))); // NOI18N
        sp_searchproductnameButton.setText("Search");
        sp_searchproductnameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sp_searchproductnameButtonActionPerformed(evt);
            }
        });
        getContentPane().add(sp_searchproductnameButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 20, 120, -1));
        getContentPane().add(sp_searchproductnameField, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 280, 20));

        sp_okButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Ok-icon.png"))); // NOI18N
        sp_okButton.setText("OK");
        sp_okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sp_okButtonActionPerformed(evt);
            }
        });
        getContentPane().add(sp_okButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 300, 100, -1));

        sp_cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Button-Close-icon.png"))); // NOI18N
        sp_cancelButton.setText("Cancel");
        sp_cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sp_cancelButtonActionPerformed(evt);
            }
        });
        getContentPane().add(sp_cancelButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 330, 100, -1));

        sp_resetviewButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/giormi/sources/Button-Reload-icon.png"))); // NOI18N
        sp_resetviewButton.setText("Reset View");
        sp_resetviewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sp_resetviewButtonActionPerformed(evt);
            }
        });
        getContentPane().add(sp_resetviewButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 50, 120, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sp_cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sp_cancelButtonActionPerformed
        // TODO add your handling code here:
        frame.setEnabled(true);
        this.setVisible(false);
        this.dispose();
}//GEN-LAST:event_sp_cancelButtonActionPerformed

    private void sp_okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sp_okButtonActionPerformed
        // TODO add your handling code here:
//        warningFrame wa = new warningFrame("selectproducterror", this);
//        wa.setVisible(true);

        frame.setEnabled(true);

        if(stockorder.equalsIgnoreCase("stock")){
            stock = (addStock)frame;
            stock.setcombox((String)productnameTable.getValueAt(productnameTable.getSelectedRow(), 0));
        }
        else {
            order = (addOrder)frame;
            order.setcombox((String)productnameTable.getValueAt(productnameTable.getSelectedRow(), 0));
        }

        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_sp_okButtonActionPerformed

//GEN-FIRST:event_searchstockButton3ActionPerformed

//GEN-LAST:event_searchstockButton3ActionPerformed

    private void sp_resetviewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sp_resetviewButtonActionPerformed
        // TODO add your handling code here:
        display();
    }//GEN-LAST:event_sp_resetviewButtonActionPerformed

    private void sp_searchproductnameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sp_searchproductnameButtonActionPerformed
        // TODO add your handling code here:
        erase();
        connect();
        int i=0;
        try {
            SQLStatement = "SELECT DISTINCT(p_name) from product "
                    + "WHERE p_name LIKE '%"+sp_searchproductnameField.getText()+"%'";

            rset = stmt.executeQuery(SQLStatement);
            while (rset.next()) {
                productnameTable.setValueAt(rset.getString("p_name"), i, 0);
                i++;
            }
            rset.close();
            disconnect();
        } catch (SQLException ex) {
            Logger.getLogger(searchproductname_in_addstockorder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_sp_searchproductnameButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable productnameTable;
    private javax.swing.JButton sp_cancelButton;
    private javax.swing.JButton sp_okButton;
    private javax.swing.JButton sp_resetviewButton;
    private javax.swing.JButton sp_searchproductnameButton;
    private javax.swing.JTextField sp_searchproductnameField;
    // End of variables declaration//GEN-END:variables

}
